using System;
using System.Collections.Generic;
using System.Linq;
using MyApp.WebApi.Entity;
using MyApp.WebApi.ParamModel;
using MyApp.WebApi.Repository;
using MyApp.WebApi.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;


namespace Admin3000.Backend.Controllers
{
    //购物车列表
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class ShoppingsController : ControllerBase
    {
        private IRepository<Shoppings> _shoppingsRespository;
        private IRepository<Users> _usersRespository;
        private IRepository<UsersGoods> _usersGoodsRespository;
        private IRepository<Goods> _goodsRespository;
        private IRepository<Photos> _photosRespository;
        private IRepository<GoodsPhotos> _goodsPhotosRespository;
        private IRepository<OrdersSpecs> _orderSpecsRespository;
        private IRepository<GoodsSpecs> _goodsSpecsRespository;
        private IRepository<Attributes> _attributesRespository;
        private IRepository<AttributeValues> _attributeValuesRespository;











        private TokenParameter _tokenParameter;

        private readonly IConfiguration _configuration;

        public ShoppingsController(
            IConfiguration configuration,
            IRepository<Shoppings> shoppingsRespository,
            IRepository<Goods> goodsRespository,
            IRepository<UsersGoods> usersGoodsRespository,
            IRepository<Photos> photosRespository,
            IRepository<GoodsPhotos> goodsPhotosRespository,
            IRepository<OrdersSpecs> orderSpecsRespository,
            IRepository<GoodsSpecs> goodsSpecsRespository,
            IRepository<Attributes> attributesRespository,
            IRepository<AttributeValues> attributeValuesRespository,
            IRepository<Users> usersRespository


        )
        {
            _configuration = configuration;
            _shoppingsRespository = shoppingsRespository;
            _usersRespository = usersRespository;
            _usersGoodsRespository = usersGoodsRespository;
            _goodsRespository = goodsRespository;
            _photosRespository = photosRespository;
            _goodsPhotosRespository = goodsPhotosRespository;
            _orderSpecsRespository = orderSpecsRespository;
            _goodsSpecsRespository = goodsSpecsRespository;
            _attributesRespository = attributesRespository;
            _attributeValuesRespository = attributeValuesRespository;

            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }


        //获取购物车列表
        [HttpGet, Route("getlist")]
        public dynamic GetList()
        {


            var list = from shoppings in _shoppingsRespository.Table
                       join users in _usersRespository.Table on shoppings.Id equals users.ShoppingsId
                       join usersGoods in _usersGoodsRespository.Table on users.Id equals usersGoods.UsersId
                       join goods in _goodsRespository.Table on usersGoods.GoodsId equals goods.Id
                       join goodsPhotos in _goodsPhotosRespository.Table on goods.Id equals goodsPhotos.GoodsId
                       join photos in _photosRespository.Table on goodsPhotos.PhotosId equals photos.Id
                       join orderSpecs in _orderSpecsRespository.Table on goods.Id equals orderSpecs.GoodsId
                       join goodSpecs in _goodsSpecsRespository.Table on orderSpecs.GoodsSpecsId equals goodSpecs.Id
                       join attributes in _attributesRespository.Table on goodSpecs.AttributesId equals attributes.Id
                       join attributevalues in _attributeValuesRespository.Table on goodSpecs.AttributeValuesId equals attributevalues.Id
                       where shoppings.IsDeleted == false
                       select new
                       {
                           ShoppingsId = shoppings.Id,
                           UsersId = users.Id,
                           Username = users.Username,
                           GoodsId = goods.Id,
                           PhotosId=photos.Id,
                           GoodsPhotos = photos.Picture,
                           GoodsName = goods.GoodsName,
                           GoodsDescription = goods.Description,
                           GoodsCurrent = goods.CurrentPrices,
                           GoodsNumber = goods.Number,
                            GoodSpecsId = goodSpecs.Id,
                            AttributeName = attributes.AttributeName,
                            Values = attributevalues.Values,
                           ShoppingPrice = shoppings.Price,
                           ShoppingNumber = shoppings.Number,
                           ShoppingSum = shoppings.Sum
                       };

            var res = new
            {
                COde = 200,
                Data = list,
                Msg = "获取用户信息表成功!"
            };
            return JsonHelper.Serialize(res);
        }

        //购物车表分页
        [HttpGet]
        public dynamic Get([FromQuery] Pager pager)
        {
            var pageIndex = pager.PageIndex;
            var pageSize = pager.PageSize;
            var rowsTotal = pager.RowsTotal;


            // 获得所有满足条件的记录
            var users =
                _shoppingsRespository.Table.Where(x => x.IsDeleted == false);

            //  var users = from program in _shoppingsRespository.Table
            //             join articleprogram in _articleRespository.Table on program.Id equals articleprogram.ProgramId
            //             orderby articleprogram.Id
            //             select new
            //             {
            //                 Id = program.Id,
            //                IsActived = program.IsActived,
            //                 IsDeleted = program.IsDeleted,
            //                 CreatedTime = program.CreatedTime,
            //                 Remarks = program.Remarks

            //             };

            // 对获得的记录分页
            var u =
                users.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            // 返回的数据里，带有数据和分页信息
            var res =
                new
                {
                    Code = 1000,
                    Data =
                        new
                        {
                            Data = u,
                            Pager =
                                new
                                {
                                    pageIndex,
                                    pageSize,
                                    rowsTotal = users.Count()
                                }
                        },
                    Msg = "获取购物车列表成功^_^"
                };
            return res;
        }
        //获取购物车表指定Id信息
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var AritpId = _shoppingsRespository.GetById(id);

            if (AritpId == null)
            {
                return new { Code = 1000, Data = "", Msg = "指定购物车不存在" };
            }
            return new
            {
                Code = 1000,
                Data = AritpId,
                Msg = "获取购物车列表成功^_^"
            };
        }

        //购物车表添加
        [HttpPost]
        public string Post(NewShoppings model)
        {

            var number = model.Number;
            var price = model.Price;
            var sum = model.Sum;

            if (
              number <= 0

            )
            {
                var tmp =
                    new
                    {
                        Code = 1004,
                        Data = "",
                        Msg = "购买数量不能为空，请确认后重试"
                    };
                return JsonHelper.Serialize(tmp);
            }

            var user =
                new Shoppings
                {
                    Number = model.Number,
                    Price = model.Price,
                    Sum = model.Sum


                };

            _shoppingsRespository.Insert(user);

            var res = new { Code = 1000, Data = user, Msg = "添加购物车成功" };
            return JsonHelper.Serialize(res);
        }
        //购物车表修改指定id
        [HttpPut("{id}")]
        public dynamic Put(int id, NewShoppings model)
        {
            var ShoppingsId = _shoppingsRespository.GetById(id);

            if (ShoppingsId != null)
            {
                ShoppingsId.Number = model.Number;
                ShoppingsId.Price = model.Price;
                ShoppingsId.Sum = model.Sum;

                // user.Password = model.Password;
                ShoppingsId.UpdatedTime = DateTime.Now;
                _shoppingsRespository.Update(ShoppingsId);
                return JsonHelper
                    .Serialize(new
                    {
                        Code = 1000,
                        Data = ShoppingsId,
                        Msg =
                            string
                                .Format("你修改购物车的id为：{0}，已经修改成功，请注意查收",
                                id)
                    });
            }
            else
            {
                return JsonHelper
                    .Serialize(new
                    {
                        Code = 104,
                        Data = "",
                        Msg = "指定Id的购物车不存在，请确认后重试"
                    });
            }
        }
        //购物车表删除
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            var AritpId = _shoppingsRespository.GetById(id);
            if (AritpId != null)
            {
                _shoppingsRespository.Delete(id);
                return new
                {
                    Code = 1000,
                    Msg = string.Format("删除指定id为{0}的购物车成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "指定Id的购物车不存在，请确认后重试"
                };
            }
        }
    }
}
